package net.wantongtech.xa.uploadbasicinfo.service;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.Files;
import com.wtkj.vo.dzzf.base.xxjh.AnnualFullCardList;
import com.wtkj.zwxt.clearing.vo.annual.AnnualCardListReqModel;
import lombok.extern.slf4j.Slf4j;
import net.wantongtech.xa.uploadbasicinfo.dao.CommonBaseDao;
import net.wantongtech.xa.uploadbasicinfo.dao.ReCreateFullAnnualListDao;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 重新创建包年全量名单数据
 */
@Slf4j
@Service
public class ReCreateFullAnnualListService {

    @Resource
    private CommonBaseDao commonBaseDao;

    @Resource
    private ReCreateFullAnnualListDao reCreateFullAnnualListDao;

    private ObjectMapper objectMapper = new ObjectMapper();

    @SuppressWarnings("UnstableApiUsage")
    public void reCreate() {
        log.info("重新创建包年全量名单数据");
        try {
            File file = new File("D:\\annualCardList\\2020-05-11\\UPLOAD_CPUCARD_20200511204025.json");
            String fileName = file.getName();
            String annualJson = Files.toString(file, StandardCharsets.UTF_8);
            JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, AnnualCardListReqModel.class);
            List<AnnualCardListReqModel> modelList = objectMapper.readValue(annualJson, javaType);


            reCreateFullAnnualListDao.deleteAnnualCardList();

            Date nowDateTime = new Date();
            DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");


            //保存包年卡全量名单
            List<AnnualFullCardList> annualFullCardLists = modelList
                    .stream()
                    .map(reqModel -> {
                        AnnualFullCardList fullCardList = new AnnualFullCardList();
                        fullCardList.setCardNo(reqModel.getCpuCardId());
                        fullCardList.setClientNo(reqModel.getBranchId());
                        fullCardList.setPacketCaptureRoad(reqModel.getPacketCaptureRoad());
                        fullCardList.setPacketCaptureStartOn(LocalDate.parse(reqModel.getPacketCaptureStartOn(), dateTimeFormatter).toDate());
                        fullCardList.setPacketCaptureEndOn(LocalDate.parse(reqModel.getPacketCaptureEndOn(), dateTimeFormatter).toDate());
                        fullCardList.setCreateTime(nowDateTime);
                        fullCardList.setReqFileName(fileName);
                        return fullCardList;
                    }).collect(Collectors.toList());
            annualFullCardLists.forEach(annualFullCardList ->
                    commonBaseDao.createValueObject(annualFullCardList));

        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }
}
